#coding=utf-8
from django.db import models
from django.forms import ModelForm
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.http import Http404
from models import users
from zhaokelai.common import skinFolder,checkLogin,admin_authenticated

class usersForm(ModelForm):
    class Meta:
        model = users
        
@admin_authenticated
def users_add(request):
    if request.method == 'POST': 
        form = usersForm(request.POST)
        if form.is_valid(): 
            newuser=form.save()
            return HttpResponseRedirect('/users/') # Redirect after POST
    else:
        form = usersForm() # An unbound form

    return render_to_response(skinFolder(request)+'users/users_add.html',{
        'form': form,
        'action':'添加',
    })

@admin_authenticated   
def users_modify(request,id):
    if id :
        id=int(id)
        user = users.objects.get(pk=id)
        if request.method == 'POST': 
            form = usersForm(request.POST, instance=user)
            newuser=form.save()
            return HttpResponseRedirect('/users/') # Redirect after POST
        else:
            form = usersForm(instance=user)
            return render_to_response(skinFolder(request)+'users/users_add.html',{'form': form,'action':'修改',})
    else:
        raise Http404()

@admin_authenticated   
def users_delete(request,id=None):
    if id :
        id=int(id)
        iuser = users.objects.get(pk=id)
        iuser.delete()
    elif request.method == 'POST':       
        ids=request.POST.getlist("tid")
        if ids and len(ids)>0:
            idstring = ','.join(ids)
            users.objects.extra(where=['id IN ('+idstring+')']).delete()
    else:
        pass
    return HttpResponseRedirect('/users/list')

def authenticate(uname,pwd):
    try:
        user = users.objects.filter(username__iexact=uname,password__iexact=pwd,valid=True).order_by("-id")[0:1].get()
    except:
        return None
    if user:
        return user
    else:
        return None
@checkLogin    
def myprofile(request):
    _user = request.session.get('user', None)
    _iuser = users.objects.get(pk=int(_user))
    if _iuser:
        return render_to_response(skinFolder(request)+'users/myprofile.html',{'user_info':_iuser,})
    else:
        return render_to_response('permissions.html', {'message':'用户资料丢失','REFERER':request.META['HTTP_REFERER']})


class myProfileForm(ModelForm):
    class Meta:
        model = users 
        exclude = ('permissions','valid')   
@checkLogin   
def edit_myprofile(request):
    _user = request.session.get('user', None)
    _iuser = users.objects.get(pk=int(_user))
    if request.method == 'POST': 
        form = myProfileForm(request.POST, instance=_iuser)
        newuser=form.save()
        return HttpResponseRedirect('/users/myprofile') # Redirect after POST
    else:
        form = myProfileForm(instance=_iuser)
        return render_to_response(skinFolder(request)+'users/users_add.html',{'form': form,'action':'修改',})
  